/**
 * @Title: ItemController
 * @Package cn.com.yiqi.wsc.api.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-林威
 * @date 2017/2/21 17:34
 * @version V1.0
 */
package cn.com.yiqi.wsc.api.controller;

import cn.com.yiqi.wsc.Ro.ItemDetailVO;
import cn.com.yiqi.wsc.Ro.ItemInfoVO;
import cn.com.yiqi.wsc.api.controller.ro.CommonRO;
import cn.com.yiqi.wsc.common.BaseQueryDO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.item.AdminItemDao;
import cn.com.yiqi.wsc.dao.shop.ItemMapper;
import cn.com.yiqi.wsc.entity.customer.Customer;
import cn.com.yiqi.wsc.entity.item.ItemDTO;
import cn.com.yiqi.wsc.entity.shop.Item;
import cn.com.yiqi.wsc.service.SMS.SmsService;
import cn.com.yiqi.wsc.service.customer.CustomerService;
import cn.com.yiqi.wsc.service.customer.impl.CartServiceImpl;
import cn.com.yiqi.wsc.service.item.PortalItemService;
import cn.com.yiqi.wsc.service.item.enumerate.ItemKindEnum;
import cn.com.yiqi.wsc.service.item.impl.ItemServiceImpl;
import cn.com.yiqi.wsc.service.order.impl.OrderServiceImpl;
import cn.com.yiqi.wsc.service.sysparams.SysParamsService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
 *
 *@ClassName: ItemController
 * Description: TODO
 * @author 会员系统组-林威
 * @date 2017/2/21 17:34
 *
 */
@ResponseBody
@Controller
@RequestMapping("/api/item")
public class ApiItemController {
    @Autowired
    ItemServiceImpl itemService;

    @Autowired
    CustomerService customerService;

    @Autowired
    ItemMapper itemDao;

    @Autowired
    OrderServiceImpl orderService;
    @Autowired
    CartServiceImpl cartService;
    @Autowired
    SmsService smsService;

    @Autowired
    AdminItemDao adminItemDao;
    @Autowired
    SysParamsService paramsService;
    /**
     * 立即购买
     * */
    @RequestMapping("/buy")
    public Result buyNow(@RequestBody CommonRO ro){
        Result re = new Result();
        ItemInfoVO vo = itemService.findItemById(ro.getItemId());
        re.putData("item",vo);
        return re;
    }

    /**
     * 购买vip
     * */
    @RequestMapping("/buyVip")
    public Result buyVip(@RequestBody CommonRO ro){
        Item item = itemService.findByid(ro.getItemId());
        Result<Customer> re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re) || item.getKind() != 1){
            return ResultUtil.fail(re,"参数错误");
        }
        //检查用户是否绑定手机号，未绑定手机号不能生产订单
        if(StringUtils.isEmpty(re.getReturnValue().getTel())){
            return ResultUtil.fail(re,"您还未绑定手机，不能购买商品");
        }

        Customer customer = customerService.selectById(ro.getUserId());
        //只能积分支付
        if(item.getPayType() == 2){
            if(customer.getPoint() < item.getPointPrice()){
                return ResultUtil.fail(re,"积分不够");
            }
            if(StringUtils.isEmpty(ro.getCode()) || !smsService.validateCode(re.getReturnValue().getTel(),ro.getCode())){
                return ResultUtil.fail(re,"验证码错误");
            }
            //　创建积分订单并支付
            orderService.createVipOrder(ro.getUserId(),ro.getItemId(),item.getOrgId());
            return ResultUtil.success(re);
        }else {
            //余额+积分支付
            //创建订单
            re = orderService.buyItem(ro.getUserId(),ro.getItemId(),1,null,item.getOrgId());
            return re;
        }
        //return re;
    }
    /**
     * 购买积分商品
     * */
    @RequestMapping("pointBuy")
    public Result buyPointItem(@RequestBody CommonRO ro){
        Item item = itemService.findByid(ro.getItemId());
        Result<Customer> re = customerService.isExistUserAndAddress(ro.getUserId(),ro.getAddressId());
        if(!ResultUtil.isSuccess(re) || item == null || item.getKind() != 3){
            return ResultUtil.fail(re,"参数错误");
        }
        //检查用户是否绑定手机号，未绑定手机号不能生产订单
        if(StringUtils.isEmpty(re.getReturnValue().getTel())){
            return ResultUtil.fail(re,"您还未绑定手机，不能购买商品");
        }
        Customer customer = re.getReturnValue();
        if(customer.getPoint() < item.getPointPrice()){
            return ResultUtil.fail(re,"积分不够");
        }
        if(StringUtils.isEmpty(ro.getCode()) || !smsService.validateCode(re.getReturnValue().getTel(),ro.getCode())){
            return ResultUtil.fail(re,"验证码错误");
        }
        //判断库存和限购
        if(item.getStock() == null || item.getStock() < 0){
            return ResultUtil.fail(re,"库存不足");
        }
        ItemDTO itemDTO = adminItemDao.getDetailByItemId(item.getId());
        if(itemDTO.getLimit() == 1 &&  itemDTO.getLimitNumber() <= itemDao.sumHasBuy(itemDTO.getId(),ro.getUserId())){
            return ResultUtil.fail(re,"超过限购");
        }

        //　创建积分订单并支付
        orderService.createPointOrder(ro.getUserId(),ro.getItemId(),ro.getAddressId());
        return ResultUtil.success(re);

    }


    /**
     * 立即购买结算
     * */
    @RequestMapping("/pay")
    public Result pay(@RequestBody CommonRO ro){

        Result<Customer> re;
        //检查收货地址
        re = customerService.isExistUserAndAddress(ro.getUserId(),ro.getAddressId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        //检查用户是否绑定手机号，未绑定手机号不能生产订单
        if(StringUtils.isEmpty(re.getReturnValue().getTel())){
            return ResultUtil.fail(re,"您还未绑定手机，不能购买商品");
        }
        Item item = itemDao.selectByPrimaryKey(ro.getItemId());
        if(null == ro.getCount() || ro.getCount() < 0 ){
            return ResultUtil.fail(re,item.getName()+"件数不能小于1件！");
        }
        if(item.getStock() < ro.getCount()){
            return  ResultUtil.fail(re,item.getName()+"库存不足，现存"+item.getStock()+"件");
        }
        re = orderService.buyItem(ro.getUserId(),ro.getItemId(),ro.getCount(),ro.getAddressId(),item.getOrgId());

        return re;
    }

    /**
     * 商品详情
     * */
    @RequestMapping("/detail")
    public Result detail(@RequestBody CommonRO ro){
        Result re = new Result();
        Item item = itemService.findByid(ro.getId());
        Integer itemId = ro.getId();
        if(item.getKind() == ItemKindEnum.point.getValue()){
            itemId = item.getRefItemId();
        }
        ItemDetailVO itemDetailVO = itemService.findItemDetail(itemId,ro.getUserId());
        if(item.getKind() == ItemKindEnum.point.getValue()){
            itemDetailVO.setId(item.getId());
            itemDetailVO.setPointPrice(item.getPointPrice());
            itemDetailVO.setType(3);
            itemDetailVO.setNowPrice(item.getSalePrice());
            itemDetailVO.setSoldCount(item.getSoldCount());
        }
        re.putData("item",itemDetailVO);
        re.putData("cartCount",cartService.findAll(ro.getUserId()).size());
        return ResultUtil.success(re);
    }

    /**
     * 热门商品
     * */
    @RequestMapping("/hot")
    public Result hot(){
        Result re = new Result();
        // 热卖商品
        List<ItemInfoVO> list = itemService.findHotItem();
        re.putData("list",list);
        return re;
    }

    /**
     * 收藏
     * */
    @RequestMapping("/like")
    public Result likeItem(@RequestBody CommonRO ro){
        Result re;
        re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        //插入收藏数据
        customerService.like(ro.getUserId(),ro.getItemId());
        //更新收藏量
        itemDao.addLike(ro.getItemId());
        return ResultUtil.success(re);
    }
    /**
     * 取消收藏
     * */
    @RequestMapping("/cancelLike")
    public Result cancelLikeItem(@RequestBody CommonRO ro){
        Result re;
        re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        //删除收藏数据
        if(customerService.deletelike(ro.getUserId(),ro.getItemId()) == 1){
            //更新收藏量
            itemDao.decreaseLike(ro.getItemId());
        }
        return ResultUtil.success(re);
    }

    /**
     * 收藏列表
     * */
    @RequestMapping("/likeList")
    public Result likeList(@RequestBody CommonRO ro){
        Result re;
        re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        if(null == ro.getPageSize()){
            ro.setPageSize(10);
            ro.setCurrentPage(1);
        }
        PageInfo<ItemInfoVO> page = itemService.selectLikeItemInfo(ro.getUserId(),ro.getPageSize(),ro.getCurrentPage());
        re.putData("data",page);
        return re;
    }

    /**
     * 积分商品
     * */
    @RequestMapping("/pointItem")
    public Result pointItem(){
        Result re = new Result();
        re.putData("data",itemService.findPointItems());
        return re;
    }

    /**
     * 开通会员
     * */
    @RequestMapping("/vipItem")
    public Result vipItem(){
        Result re = new Result();
        re.putData("data",itemService.findVipItems());
        return re;
    }

    /**
     * 系统qq
     * */
    @RequestMapping("/sysqq")
    public Result sysQQ(){
        Result re = new Result();
        re.putData("data",paramsService.getSysQQ());
        return re;
    }

}
